﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cairo;

namespace BTCairo
{
    public class Cairo : CommonGraphics
    {
        Surface s;
        public Surface S
        {
            get { return s; }
            set { s = value; }
        }
        Context c;
        public Context C
        {
            get { return c; }
            set { c = value; }
        }

        public Cairo(System.Drawing.Graphics _g)
        {
            myGraphic = _g;
            s = new Win32Surface(myGraphic.GetHdc());
            c = new Context(s);
            //c.SetSourceRGB(1, 0, 0);   
        }
        public Cairo(System.Drawing.Graphics _g, InfoGraphic info)
        {
            myGraphic = _g;
            s = new Win32Surface(myGraphic.GetHdc());
            c = new Context(s);

            this.SetInfoGraphic(info);
        }
        public Cairo(Cairo temp)
        {
            myGraphic = temp.myGraphic;
            s = temp.s;
            c = temp.c;
            this.SetInfoGraphic(temp.MyInfoGraphic);
        }
        public override CommonGraphics CloneCommonGraphic()
        {
            //InfoGraphic info = new InfoGraphic(MyInfoGraphic);
            //Cairo clone = new Cairo(myGraphic, info);
            Cairo clone = new Cairo(this);
            return clone;
        }
        public override void SetInfoGraphic(InfoGraphic info)
        {
            MyInfoGraphic = new InfoGraphic(info);
            c.LineWidth = info.width;
            c.SetSourceRGB((float)info.lineRed / 255, (float)info.lineGreen / 255, (float)info.lineBlue / 255);
        }


        public override void DrawLine(double x1, double y1, double x2, double y2)
        {
            c.SetSourceRGB((float)MyInfoGraphic.lineRed / 255, (float)MyInfoGraphic.lineGreen / 255, (float)MyInfoGraphic.lineBlue / 255);
            c.MoveTo(x1, y1);
            c.LineTo(x2, y2);
            c.Stroke();
            //G.ReleaseHdc(s.Handle);
        }

        public override void DrawRect(double x, double y, double width, double height)
        {
            c.SetSourceRGB((float)MyInfoGraphic.lineRed / 255, (float)MyInfoGraphic.lineGreen / 255, (float)MyInfoGraphic.lineBlue / 255);
            c.Rectangle(x, y, width, height);
            c.StrokePreserve();
            if(MyInfoGraphic.fill)
            {
                c.SetSourceRGB((float)MyInfoGraphic.fillRed / 255, (float)MyInfoGraphic.fillGreen / 255, (float)MyInfoGraphic.fillBlue / 255);
                c.Fill();
                c.SetSourceRGB((float)MyInfoGraphic.lineRed / 255, (float)MyInfoGraphic.lineGreen / 255, (float)MyInfoGraphic.lineBlue / 255);
            }
         
        }

        public override void DrawEllipse(double x, double y, double width, double height)
        {
            c.SetSourceRGB((float)MyInfoGraphic.lineRed / 255, (float)MyInfoGraphic.lineGreen / 255, (float)MyInfoGraphic.lineBlue / 255);
            c.Scale(1, 1);
            c.Arc(x, y, (width < height ? width : height) / 2 - 10, 0, 2 * Math.PI);
            c.StrokePreserve();
            if (MyInfoGraphic.fill)
            {
                c.SetSourceRGB((float)MyInfoGraphic.fillRed / 255, (float)MyInfoGraphic.fillGreen / 255, (float)MyInfoGraphic.fillBlue / 255);
                c.Fill();
                c.SetSourceRGB((float)MyInfoGraphic.lineRed / 255, (float)MyInfoGraphic.lineGreen / 255, (float)MyInfoGraphic.lineBlue / 255);
            }
        }

        public override void DrawText(string text, int size, int x, int y)
        {
            c.SetSourceRGB((float)MyInfoGraphic.lineRed / 255, (float)MyInfoGraphic.lineGreen / 255, (float)MyInfoGraphic.lineBlue / 255);
            c.SetFontSize((double)size);
            c.MoveTo((double)x, (double)y);
            c.ShowText(text);
        }
    }
}
